<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🔥 连板天梯 - 股票选股系统</title>
    <link rel="stylesheet" href="/static/base.css">
    <link rel="stylesheet" href="/static/css/common.css">
    <link rel="stylesheet" href="/static/css/dark-theme.css">
    <link href="/static/lib/css/all.min.css" rel="stylesheet">
    <style>
        /* 使用与其他页面一致的背景 */
        body { 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #333;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            line-height: 1.6;
        }
        
        .consecutive-container { 
            max-width: 1600px; 
            margin: 0 auto; 
            padding: 20px; 
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        /* 头部样式 */
        .header { 
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 20px;
            margin-bottom: 15px;
            text-align: center;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            width: 100%;
        }
        
        .header h1 { 
            font-size: 2.5em;
            background: linear-gradient(135deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 10px;
            font-weight: 700;
        }
        
        .header p { color: #666; font-size: 1.1em; margin-bottom: 20px; }
        
        /* 主要内容区域 */
        .consecutive-main-content { 
            display: grid; 
            grid-template-columns: 300px 1fr; 
            gap: 20px; 
            margin-bottom: 20px;
            width: 100%;
        }
        
        /* 左侧连板天梯 */
        .ladder-section { 
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            height: fit-content;
            transition: all 0.3s ease;
        }
        
        .ladder-section:hover {
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        
        .ladder-title { 
            font-size: 18px; 
            font-weight: 700; 
            color: #333; 
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .ladder-subtitle { 
            font-size: 14px; 
            color: #666; 
            margin-bottom: 20px; 
            padding-bottom: 12px; 
            border-bottom: 1px solid #e1e5e9; 
        }
        
        .ladder-item { 
            display: flex; 
            justify-content: space-between; 
            align-items: center; 
            padding: 12px 16px; 
            margin-bottom: 8px; 
            background: #f8f9fa; 
            border-radius: 8px; 
            border: 1px solid #e1e5e9; 
            transition: all 0.3s ease; 
            cursor: pointer; 
        }
        
        .ladder-item:hover { 
            background: #e9ecef; 
            border-color: #667eea; 
            transform: translateY(-1px);
        }
        
        .ladder-item.active { 
            background: #667eea; 
            border-color: #667eea; 
            color: white;
        }
        
        .ladder-days { 
            font-size: 16px; 
            font-weight: 700; 
            color: #e74c3c; 
        }
        
        .ladder-item.active .ladder-days { color: #ffffff; }
        
        .ladder-stock { 
            font-size: 14px; 
            color: #333; 
            text-align: right; 
        }
        
        .ladder-item.active .ladder-stock { color: #ffffff; }
        
        /* 右侧异动列表 */
        .anomaly-section { 
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }
        
        .anomaly-section:hover {
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        
        .anomaly-title { 
            font-size: 18px; 
            font-weight: 700; 
            color: #333; 
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        /* 数据表格 */
        .data-table { 
            width: 100%; 
            border-collapse: collapse; 
            margin-top: 16px;
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .data-table th { 
            background: #667eea; 
            color: #ffffff; 
            font-size: 12px; 
            font-weight: 600; 
            padding: 12px 8px; 
            text-align: left; 
            text-transform: uppercase; 
        }
        
        .data-table td { 
            padding: 12px 8px; 
            border-bottom: 1px solid #e1e5e9; 
            font-size: 13px; 
            color: #333; 
        }
        
        .data-table tr:hover { background: #f8f9fa; }
        
        /* 股票代码和名称 */
        .stock-code { font-weight: 700; color: #667eea; }
        .stock-name { color: #666; font-size: 12px; margin-top: 2px; }
        
        /* 涨幅 */
        .gain-percent { color: #e74c3c; font-weight: 700; }
        
        /* 现价 */
        .current-price { color: #e74c3c; font-weight: 700; }
        
        /* 涨停原因 */
        .limit-up-reason { color: #666; font-size: 12px; max-width: 200px; line-height: 1.4; }
        
        /* 涨幅样式 */
        .gain-percent.positive { color: #e74c3c; font-weight: 600; }
        .gain-percent.negative { color: #27ae60; font-weight: 600; }
        
        /* 涨停时间 */
        .limit-up-time { color: #667eea; font-family: 'Courier New', monospace; font-weight: 600; }
        
        /* 涨停状态 */
        .limit-up-status { 
            padding: 4px 8px; 
            border-radius: 4px; 
            font-size: 11px; 
            font-weight: 600; 
            text-align: center; 
        }
        
        .status-first { background: #e74c3c; color: #ffffff; }
        .status-consecutive { background: #f39c12; color: #ffffff; }
        
        /* 流通市值 */
        .market-cap { color: #666; font-size: 12px; }
        
        /* 筛选栏 */
        .filters-bar { 
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }
        
        .filters-bar:hover {
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        
        .filter-row { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
        .filter-group { display: flex; flex-direction: column; gap: 8px; }
        .filter-label { font-size: 12px; font-weight: 600; color: #333; text-transform: uppercase; }
        
        .filter-input, .filter-select { 
            padding: 8px 12px; 
            border: 1px solid #ddd; 
            border-radius: 6px; 
            font-size: 14px; 
            background: white; 
            color: #333; 
            min-width: 120px; 
        }
        
        .filter-input:focus, .filter-select:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .filter-btn { 
            padding: 8px 16px; 
            background: #667eea; 
            color: white; 
            border: none; 
            border-radius: 6px; 
            font-size: 14px; 
            font-weight: 600; 
            cursor: pointer; 
            transition: all 0.2s ease; 
        }
        
        .filter-btn:hover { 
            background: #5a6fd8; 
            transform: translateY(-1px); 
        }
        
        .filter-btn.secondary { background: #6c757d; }
        .filter-btn.secondary:hover { background: #5a6268; }
        
        /* 统计摘要 */
        .summary-stats { 
            display: grid; 
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); 
            gap: 16px; 
            margin-bottom: 20px; 
        }
        
        .summary-card { 
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }
        
        .summary-card:hover {
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        
        .summary-value { 
            font-size: 28px; 
            font-weight: 700; 
            color: #667eea; 
            margin-bottom: 8px; 
        }
        
        .summary-label { 
            font-size: 14px; 
            color: #666; 
            text-transform: uppercase; 
            font-weight: 600; 
        }
        
        .loading, .no-data, .error { 
            text-align: center; 
            padding: 40px; 
            color: #666; 
        }
        
        .error { color: #e74c3c; }
        
        @media (max-width: 1024px) { 
            .main-content { grid-template-columns: 1fr; } 
        }
    </style>
</head>
<body>
    <div class="consecutive-container">
        <div class="header">
            <div class="header-nav">
                <h1><i class="fas fa-fire"></i> 连板天梯</h1>
                <div class="header-nav-links"></div>
            </div>
            <p>按连板次数分组展示涨停股票，把握市场热点梯队</p>
        </div>

        <div class="filters-bar">
            <div class="filter-row">
                <div class="filter-group">
                    <label class="filter-label">交易日期</label>
                    <input type="date" id="tradeDate" class="filter-input" value="">
                </div>
                <div class="filter-group">
                    <label class="filter-label">股票代码</label>
                    <input type="text" id="securityId" class="filter-input" placeholder="输入股票代码">
                </div>
                <div class="filter-group">
                    <label class="filter-label">股票名称</label>
                    <input type="text" id="securityName" class="filter-input" placeholder="输入股票名称">
                </div>
                <div class="filter-group">
                    <label class="filter-label">市场</label>
                    <select id="market" class="filter-select">
                        <option value="">全部</option>
                        <option value="1">上海</option>
                        <option value="2">深圳</option>
                    </select>
                </div>
                <div class="filter-group">
                    <button onclick="searchConsecutiveLimitUp()" class="filter-btn">
                        <i class="fas fa-search"></i> 搜索
                    </button>
                </div>
                <div class="filter-group">
                    <button onclick="resetFilters()" class="filter-btn secondary">
                        <i class="fas fa-redo"></i> 重置
                    </button>
                </div>
            </div>
        </div>

        <div class="summary-stats" id="summaryStats">
            <div class="summary-card">
                <div class="summary-value" id="totalCount">-</div>
                <div class="summary-label">涨停总数</div>
            </div>
            <div class="summary-card">
                <div class="summary-value" id="maxConsecutive">-</div>
                <div class="summary-label">最高连板</div>
            </div>
            <div class="summary-card">
                <div class="summary-value" id="avgConsecutive">-</div>
                <div class="summary-label">平均连板</div>
            </div>
            <div class="summary-card">
                <div class="summary-value" id="totalTurnover">-</div>
                <div class="summary-label">总成交额(亿)</div>
            </div>
        </div>

        <div class="consecutive-main-content">
            <div class="ladder-section">
                <div class="ladder-title">
                    <i class="fas fa-fire"></i> 连板天梯
                </div>
                <div class="ladder-subtitle">连板高度</div>
                <div id="ladderList">
                    <div class="loading">
                        <i class="fas fa-spinner fa-spin"></i> 加载中...
                    </div>
                </div>
            </div>

            <div class="anomaly-section">
                <div class="anomaly-title">
                    <i class="fas fa-chart-line"></i> 异动列表
                </div>

                <div id="dataTable">
                    <div class="loading">
                        <i class="fas fa-spinner fa-spin"></i> 加载中...
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentFilters = {};
        let currentLadderSelection = null;

        document.addEventListener('DOMContentLoaded', function() {
            const today = new Date().toISOString().split('T')[0];
            document.getElementById('tradeDate').value = today;
            loadConsecutiveLimitUpData();
        });

        async function loadConsecutiveLimitUpData() {
            const ladderContainer = document.getElementById('ladderList');
            const tableContainer = document.getElementById('dataTable');
            
            ladderContainer.innerHTML = '<div class="loading"><i class="fas fa-spinner fa-spin"></i> 加载中...</div>';
            tableContainer.innerHTML = '<div class="loading"><i class="fas fa-spinner fa-spin"></i> 加载中...</div>';

            try {
                const params = new URLSearchParams({...currentFilters});
                const response = await fetch(`/api/consecutive-limit-up/tiers?${params}`);
                const result = await response.json();

                if (result.success) {
                    renderLadderList(result.data);
                    renderDataTable(result.data);
                    updateSummaryStats(result.data);
                } else {
                    const errorMsg = `<div class="error"><i class="fas fa-exclamation-triangle"></i> ${result.message || '加载失败'}</div>`;
                    ladderContainer.innerHTML = errorMsg;
                    tableContainer.innerHTML = errorMsg;
                }
            } catch (error) {
                console.error('加载连板数据失败:', error);
                const errorMsg = '<div class="error"><i class="fas fa-exclamation-triangle"></i> 网络错误，请稍后重试</div>';
                ladderContainer.innerHTML = errorMsg;
                tableContainer.innerHTML = errorMsg;
            }
        }

        function renderLadderList(data) {
            const ladderContainer = document.getElementById('ladderList');
            
            if (!data.tiers || data.tiers.length === 0) {
                ladderContainer.innerHTML = '<div class="no-data"><i class="fas fa-inbox"></i> 暂无数据</div>';
                return;
            }

            const html = data.tiers.map(tier => {
                const isActive = currentLadderSelection === tier.consecutive_days;
                const firstStock = tier.stocks.length > 0 ? tier.stocks[0] : null;
                const stockCount = tier.stocks.length;
                
                // 连板天梯只显示连板股票，连板次数就是实际的连板天数
                const displayText = `${tier.consecutive_days}连板`;
                
                return `
                    <div class="ladder-item ${isActive ? 'active' : ''}" 
                         onclick="selectLadder(${tier.consecutive_days})">
                        <div class="ladder-days">${displayText}</div>
                        <div class="ladder-stock">${firstStock ? firstStock.security_name : '无'} (${stockCount}只)</div>
                    </div>
                `;
            }).join('');

            ladderContainer.innerHTML = html;
        }

        function renderDataTable(data) {
            const tableContainer = document.getElementById('dataTable');
            
            if (!data.tiers || data.tiers.length === 0) {
                tableContainer.innerHTML = '<div class="no-data"><i class="fas fa-inbox"></i> 暂无数据</div>';
                return;
            }

            let stocks = [];
            if (currentLadderSelection) {
                const selectedTier = data.tiers.find(tier => tier.consecutive_days === currentLadderSelection);
                if (selectedTier) {
                    stocks = selectedTier.stocks;
                }
            } else {
                data.tiers.forEach(tier => {
                    stocks = stocks.concat(tier.stocks);
                });
            }
            
            // 按首次涨停时间升序排序
            stocks.sort((a, b) => {
                const timeA = a.first_limit_up_time || '';
                const timeB = b.first_limit_up_time || '';
                return timeA.localeCompare(timeB);
            });

            if (stocks.length === 0) {
                tableContainer.innerHTML = '<div class="no-data"><i class="fas fa-inbox"></i> 暂无数据</div>';
                return;
            }

            const html = `
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>代码</th>
                            <th>涨幅%↓</th>
                            <th>现价</th>
                            <th>涨停原因</th>
                            <th>首次涨停时间</th>
                            <th>成交金额</th>
                            <th>换手率</th>
                            <th>集合竞价成交金额</th>
                            <th>流通市值</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${stocks.map(stock => `
                            <tr>
                                <td>
                                    <div class="stock-code">${stock.security_id}</div>
                                    <div class="stock-name">${stock.security_name || '未知'}</div>
                                </td>
                                <td class="gain-percent ${(stock.daily_change || 0) >= 0 ? 'positive' : 'negative'}">${(stock.daily_change || 0) >= 0 ? '+' : ''}${stock.daily_change || 0}%</td>
                                <td class="current-price">${stock.close_price || 0}</td>
                                <td class="limit-up-reason">${stock.hot_concepts || '-'}</td>
                                <td class="limit-up-time">${formatLimitUpTime(stock.first_limit_up_time)}</td>
                                <td class="market-cap">${formatAmount(stock.turnover_amount)}</td>
                                <td class="market-cap">${stock.turnover_rate || 0}%</td>
                                <td class="market-cap">${formatAmount(stock.auction_amount || 0)}</td>
                                <td class="market-cap">${formatMarketCap(stock.market_cap)}</td>
                            </tr>
                        `).join('')}
                    </tbody>
                </table>
            `;

            tableContainer.innerHTML = html;
        }

        function selectLadder(consecutiveDays) {
            currentLadderSelection = currentLadderSelection === consecutiveDays ? null : consecutiveDays;
            loadConsecutiveLimitUpData();
        }







        function updateSummaryStats(data) {
            document.getElementById('totalCount').textContent = data.total_count || 0;
            document.getElementById('maxConsecutive').textContent = data.max_consecutive || 0;
            document.getElementById('avgConsecutive').textContent = (data.avg_consecutive || 0).toFixed(1);
            document.getElementById('totalTurnover').textContent = formatAmount(data.total_turnover);
        }

        function searchConsecutiveLimitUp() {
            currentFilters = {
                trade_date: document.getElementById('tradeDate').value,
                security_id: document.getElementById('securityId').value,
                security_name: document.getElementById('securityName').value,
                market: document.getElementById('market').value
            };
            loadConsecutiveLimitUpData();
        }

        function resetFilters() {
            document.getElementById('tradeDate').value = new Date().toISOString().split('T')[0];
            document.getElementById('securityId').value = '';
            document.getElementById('securityName').value = '';
            document.getElementById('market').value = '';
            currentFilters = {};
            currentLadderSelection = null;
            loadConsecutiveLimitUpData();
        }
    </script>
    
    <script src="/static/js/common.js"></script>
    <script src="/static/js/navigation.js"></script>
</body>
</html>
